package com.kotlin.book.api

import HttpApi
import com.google.gson.reflect.TypeToken
import com.kotlin.book.entry.Comment

object CommentApi {

    fun getCommentList(
        param: Map<String, Any?>?,
        callback: (HttpApi.ApiResponse<List<Comment>>?) -> Unit
    ) {
        val type = object : TypeToken<HttpApi.ApiResponse<List<Comment>>>() {}.type
        HttpApi.get<List<Comment>>(
            "/comment/getCommentList",
            param ?: mapOf(),
            type
        ) { response ->
            response?.let { callback(it) } ?: callback(null)
        }
    }

    fun addComment(
        param: Map<String, Any?>?,
        callback: (HttpApi.ApiResponse<Any>?) -> Unit
    ) {
        val type = object : TypeToken<HttpApi.ApiResponse<Any>>() {}.type
        HttpApi.post<Any>(
            "/comment/addComment",
            param ?: mapOf(),
            type
        ) { response ->
            response?.let { callback(it) } ?: callback(null)
        }
    }

    fun getCommentCount(
        param: Map<String, Any?>?,
        callback: (HttpApi.ApiResponse<Int>?) -> Unit
    ) {
        val type = object : TypeToken<HttpApi.ApiResponse<Int>>() {}.type
        HttpApi.get<Int>(
            "/comment/getCommentCount",
            param ?: mapOf(),
            type
        ) { response ->
            response?.let { callback(it) } ?: callback(null)
        }
    }

    data class UpdatePraiseUserResponseType(
        val isPraise: Boolean
    )

    fun updatePraiseUser(
        param: Map<String, Any?>?,
        callback: (HttpApi.ApiResponse<UpdatePraiseUserResponseType>?) -> Unit
    ) {
        val type = object : TypeToken<HttpApi.ApiResponse<UpdatePraiseUserResponseType>>() {}.type
        HttpApi.post<UpdatePraiseUserResponseType>(
            "/comment/updatePraiseUser",
            param ?: mapOf(),
            type
        ) { response ->
            response?.let { callback(it) } ?: callback(null)
        }
    }
}